package com.ccqlk.basespringbootapp;

import com.ccqlk.basespringbootapp.domain.QuestionChoiceCopy;
import com.ccqlk.basespringbootapp.domain.QuestionCopy;
import com.ccqlk.basespringbootapp.domain.QuestionRelationCopy;
import com.ccqlk.basespringbootapp.service.QuestionChoiceCopyService;
import com.ccqlk.basespringbootapp.service.QuestionCopyService;
import com.ccqlk.basespringbootapp.service.QuestionRelationCopyService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@SuppressWarnings("all")
@SpringBootTest
public class 德旭题库数据处理 {
    @Resource
    private QuestionCopyService questionCopyService;
    @Resource
    private QuestionChoiceCopyService questionChoiceCopyService;
    @Resource
    private QuestionRelationCopyService questionRelationCopyService;

    @Test
    public void test (){
        Map<String,List<String>> tagMap = new LinkedHashMap<>();
        tagMap.put("NAJ1B1",Arrays.asList("DB","","N","YQ"));
        tagMap.put("NAJ1B2",Arrays.asList("DB","","N","YQ"));
        tagMap.put("NAJ1B3",Arrays.asList("DB","","N","YQ"));
        tagMap.put("NAJ1B4",Arrays.asList("DB","","N","YQ"));
        tagMap.put("NAJ1B5",Arrays.asList("N"));
        tagMap.put("NAJ1B6",Arrays.asList("N"));
        tagMap.put("NAJ1X1",Arrays.asList("N"));
        tagMap.put("NA1C1",Arrays.asList("H"));
        tagMap.put("NA1C2",Arrays.asList("H"));
        tagMap.put("NA1C3",Arrays.asList("H"));
        tagMap.put("NA1C4",Arrays.asList("H"));
        tagMap.put("NA1C5",Arrays.asList("H"));
        tagMap.put("NA1C6",Arrays.asList("H"));

        //查询套题
        LinkedHashMap<String, List<QuestionCopy>> questionNewMap = questionCopyService.lambdaQuery().like(QuestionCopy::getTag, "VVVV").like(QuestionCopy::getTag, "NA")
                .list()
                .stream()
                .collect(Collectors.groupingBy(data -> data.getTag().substring(0, 6), LinkedHashMap::new, Collectors.toList()));

        ArrayList<Map.Entry<String, List<QuestionCopy>>> entries = new ArrayList<>(questionNewMap.entrySet());


        tagMap.forEach((key,value) -> {
            value.forEach(areaCode -> {
                String finalChoice = areaCode + key;
                String substringIndexStr = finalChoice.substring(finalChoice.length() - 1);
                int parseInt = Integer.parseInt(substringIndexStr);
                // 删除题库与题的关联关系
                questionRelationCopyService.lambdaUpdate().eq(QuestionRelationCopy::getChoice, finalChoice).remove();

                // 不是1-4，直接删除题库自身
                if(finalChoice.contains("NAJ1X1") || parseInt > 4){
                    log.info("need del:{}",finalChoice);
                    questionChoiceCopyService.lambdaUpdate().eq(QuestionChoiceCopy::getTag, finalChoice).remove();
                }
                // 是1-4，重建关联关系
                else {
                    // 删除原来的关联关系
                    Map.Entry<String, List<QuestionCopy>> stringListEntry = entries.get(parseInt - 1);
                    List<QuestionCopy> questionList = stringListEntry.getValue();
                    List<QuestionRelationCopy> newRelationList = new ArrayList<>();
                    for (int i = 0; i < questionList.size(); i++) {

                        QuestionRelationCopy questionRelationCopy = new QuestionRelationCopy();
                        questionRelationCopy.setChoice(finalChoice);
                        questionRelationCopy.setTag(questionList.get(i).getTag());
                        questionRelationCopy.setSort(i + 1);
                        questionRelationCopy.setCreateTime(new Date());
                        newRelationList.add(questionRelationCopy);
                    }

                    questionRelationCopyService.saveBatch(newRelationList);
                }



            });
        });
    }

//    @Test
//    public void test() throws JsonProcessingException {
//        List<String> allQuestionChoice = Arrays.asList("GS1C1","GS1C2","GS1C3","GS1C4","GS1C5","GS1C6","GS1C7");
//
//        // 查询正确的题库编号
//        Map<String, List<QuestionRelationCopy>> choiceTagMap = questionRelationCopyService.lambdaQuery().in(QuestionRelationCopy::getChoice, allQuestionChoice).list()
//                .stream()
//                .collect(Collectors.groupingBy(QuestionRelationCopy::getChoice, LinkedHashMap::new, Collectors.toList()));
//        ObjectMapper objectMapper = new ObjectMapper();
//        List<String> areaList = Arrays.asList("YQS1B1","YQS1B2","YQS1B3","YQS1B4","YQS1B5","YQS1B6","YQS1B7");
//        ArrayList<Map.Entry<String, List<QuestionRelationCopy>>> entryArrayList = new ArrayList<>(choiceTagMap.entrySet());
//
//        for (int i = 0; i < entryArrayList.size(); i++) {
//            Map.Entry<String, List<QuestionRelationCopy>> stringListEntry = entryArrayList.get(i);
//            List<QuestionRelationCopy> tagList = stringListEntry.getValue();
//            String choice = stringListEntry.getKey();
//
//            String needChangeTag = areaList.get(i);
//
//            // 删除这个tag原来的关联的题
//            boolean remove = questionRelationCopyService.lambdaUpdate().eq(QuestionRelationCopy::getChoice, needChangeTag).remove();
//            log.info("tag:{} remove old relation,ok?:{}", needChangeTag, remove);
//
//            // 查询是否有这个题库，没有就新增
//            Long count = questionChoiceCopyService.lambdaQuery().eq(QuestionChoiceCopy::getTag, needChangeTag).count();
//            if(count == 0){
//                QuestionChoiceCopy questionChoiceCopy = questionChoiceCopyService.lambdaQuery().eq(QuestionChoiceCopy::getTag, choice).one();
//                questionChoiceCopy.setId(null);
//                questionChoiceCopy.setTag(needChangeTag);
//
//                questionChoiceCopyService.save(questionChoiceCopy);
//            }
//
//            // 将现在新的题库，关联到这个需要修改的tag
//            String tagListString = objectMapper.writeValueAsString(tagList);
//            List<QuestionRelationCopy> relationList = objectMapper.readValue(tagListString, new TypeReference<>() {});
//            for (QuestionRelationCopy questionRelationCopy : relationList) {
//                questionRelationCopy.setId(null);
//                questionRelationCopy.setChoice(needChangeTag);
//            }
//            questionRelationCopyService.saveBatch(relationList);
//
//        }

//
//
//
//        choiceTagMap.forEach((choice,tagList) -> {
//            try {
//                String baseTag = choice.substring(1);
//                for (String needChangeTag : areaList) {
//                    // 删除这个tag原来的关联的题
//                    boolean remove = questionRelationCopyService.lambdaUpdate().eq(QuestionRelationCopy::getChoice, needChangeTag).remove();
//                    log.info("tag:{} remove old relation,ok?:{}", needChangeTag, remove);
//
//                    // 查询是否有这个题库，没有就新增
//                    Long count = questionChoiceCopyService.lambdaQuery().eq(QuestionChoiceCopy::getTag, needChangeTag).count();
//                    if(count == 0){
//                        QuestionChoiceCopy questionChoiceCopy = questionChoiceCopyService.lambdaQuery().eq(QuestionChoiceCopy::getTag, choice).one();
//                        questionChoiceCopy.setId(null);
//                        questionChoiceCopy.setTag(needChangeTag);
//
//                        questionChoiceCopyService.save(questionChoiceCopy);
//                    }
//
//                    // 将现在新的题库，关联到这个需要修改的tag
//                    String tagListString = objectMapper.writeValueAsString(tagList);
//                    List<QuestionRelationCopy> relationList = objectMapper.readValue(tagListString, new TypeReference<>() {});
//                    for (QuestionRelationCopy questionRelationCopy : relationList) {
//                        questionRelationCopy.setId(null);
//                        questionRelationCopy.setChoice(needChangeTag);
//                    }
//                    questionRelationCopyService.saveBatch(relationList);
//                }
//            } catch (Exception e) {
//                log.error("error", e);
//            }
//        });
//    }
}
